package mest.service.hws.impl;

import com.alibaba.fastjson.JSON;
import mest.dao.hws.WarehouseFileDao;
import mest.entity.warehouse_file;
import mest.service.hws.WarehouseFilehwsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor=Exception.class)
public class WarehouseFilehwsServiceImpl implements WarehouseFilehwsService {

    @Resource
    private WarehouseFileDao warehouseFileDao;

    @Override
    public int deleteByPrimaryKey(String theirDb,
                                  Integer id) {
        return warehouseFileDao.deleteByPrimaryKey(theirDb,id);
    }

    @Override
    public String insert(String theirDb,
                      warehouse_file record) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            warehouseFileDao.insert(theirDb,record);
            map.put("code",0);
            map.put("msg","添加成功");

        } catch (Exception e) {
            e.printStackTrace();
            if (e.getClass().getName().equals("org.springframework.dao.DuplicateKeyException")) {
                map.put("msg","编码重复");
                return JSON.toJSONString(map);
            }
            map.put("msg", "添加失败");
        }
        return JSON.toJSONString(map);
    }

    @Override
    public int insertSelective(String theirDb,
                               warehouse_file record) {
        return warehouseFileDao.insertSelective(theirDb,record);
    }

    @Override
    public warehouse_file selectByPrimaryKey(String theirDb,
                                             Integer id) {
        return warehouseFileDao.selectByPrimaryKey(theirDb,id);
    }

    @Override
    public List<warehouse_file> selectPrimary(String theirDb,
                                              Integer limit, Integer page ,String wfname) {
        return warehouseFileDao.selectPrimary(theirDb,limit,page,wfname);
    }

    @Override
    public int selectcount(String theirDb,
                           String wfname) {
        return warehouseFileDao.selectcount(theirDb,wfname);
    }

    @Override
    public int updateByPrimaryKeySelective(String theirDb,
                                           warehouse_file record) {
        return warehouseFileDao.updateByPrimaryKeySelective(theirDb,record);
    }

    @Override
    public String updateByPrimaryKey(String theirDb,
                                  warehouse_file record) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            warehouseFileDao.updateByPrimaryKey(theirDb,record);
            map.put("code",0);
            map.put("msg","添加成功");
        }catch (Exception e){
            e.printStackTrace();
            if (e.getClass().getName().equals("org.springframework.dao.DuplicateKeyException")) {
                map.put("msg","编码重复");
                return JSON.toJSONString(map);
            }
            map.put("msg","添加失败");
        }
        return JSON.toJSONString(map);
    }

}
